class_name shark
extends JiGuan
@export var speed:float =200
@export var rot_delta:float = 0
@export var order:int = 0
@export var end_pos:Vector2=Vector2.ZERO
var begin_pos := Vector2.ZERO
var to_end:bool = true
var dir:Vector2
var a =false
var vvvv = 0.0
var materials = []
func _ready() -> void:
	begin_pos = self.global_position
	dir  = end_pos.normalized()
	$Sprite2D.material = $Sprite2D.material.duplicate()
	$Sprite2D/Sprite2D.material = $Sprite2D/Sprite2D.material.duplicate()
	materials.push_back($Sprite2D.material)
	materials.push_back($Sprite2D/Sprite2D.material)
	CountManager.append_sharks(self)

func act():
	if a==false:
		a=true
	else:
		return

func _process(delta: float) -> void:
	if a:
		vvvv+=delta
		if vvvv>0.7:
			set_deferred("monitoring",true)
		elif vvvv<=0.7:
			for i in materials:
				i.set_shader_parameter("delta",vvvv)
			return
	else :
		return
	rotation_degrees+=delta*rot_delta
	var v:Vector2
	if to_end:
		v= dir*speed*delta
		global_position+=v
		if abs((global_position-begin_pos).length()-end_pos.length())<5:
			to_end=false
	else:
		v= -dir*speed*delta
		global_position+=v
		if (global_position-begin_pos).length()<5:
			to_end = true
	if v.x<0:
		scale.x = 1
	else:
		scale.x = -1

func attack_player(body):
	if body is Player:
		body.back((body.global_position-self.global_position).normalized())


func _on_body_entered(body: Node2D) -> void:
	attack_player(body)
